package com.easylinkin.linkappapi.hcmy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.hcmy.entity.CarManage;
import com.easylinkin.linkappapi.hcmy.service.CarManageService;
import com.easylinkin.linkappapi.common.model.RequestModel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

/**
 * class info :车辆列表
 *
 * @author hongyikang
 * @date 2021/8/17 9:16
 */
@RestController
@RequestMapping("/carManage")
public class CarManageController {

    @Resource
    private CarManageService carManageService;

    @PostMapping("add")
    public RestMessage add(@RequestBody @Valid CarManage carManage) {
        Assert.isTrue(!StringUtils.isEmpty(carManage.getName()), "车辆名称不能为空");
        Assert.isTrue(!StringUtils.isEmpty(carManage.getCode()), "车牌号不能为空");
        Assert.notNull(carManage.getCapacity(), "载重量不能为空");
        Assert.isTrue(!StringUtils.isEmpty(carManage.getGpsId()), "GPS设备不能为空");
        Assert.notNull(carManage.getPurpose(), "用途不能为空");

        String addResult = carManageService.add(carManage);
        if (StrUtil.isNotEmpty(addResult)) {
            return RestBuilders.failureBuilder().message(addResult).build();
        }
        return RestBuilders.successBuilder().message("成功").build();
    }

    /***
     * 获取列表
     */
    @PostMapping("getPage")
    public RestMessage getPage(@RequestBody RequestModel<CarManage> requestModel) {
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<CarManage> page = carManageService.getPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(page).build();
    }

    /***
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("get")
    public RestMessage get(Long id) {
        Assert.notNull(id, "id 不能为空");
        CarManage carManage = carManageService.getById(id);
        return RestBuilders.successBuilder().data(carManage).build();
    }

    /***
     * 修改详情
     * @param carManage 车辆
     * @return
     */
    @PostMapping("update")
    public RestMessage update(@RequestBody @Valid CarManage carManage) {
        Assert.notNull(carManage.getId(), "主键ID不能为空");
        Assert.notNull(carManage.getCapacity(), "载重量不能为空");
        Assert.notNull(carManage.getCapacity(), "载重量不能为空");
        Assert.isTrue(!StringUtils.isEmpty(carManage.getName()), "车辆名称不能为空");
        Assert.isTrue(!StringUtils.isEmpty(carManage.getCode()), "车牌号不能为空");
        Assert.isTrue(!StringUtils.isEmpty(carManage.getGpsId()), "GPS设备不能为空");
        Assert.notNull(carManage.getPurpose(), "用途不能为空");

        String updateResult = carManageService.update(carManage);
        if (StrUtil.isNotEmpty(updateResult)) {
            return RestBuilders.failureBuilder().message(updateResult).build();
        }
        return RestBuilders.successBuilder().message("成功").build();
    }

    /***
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delete")
    public RestMessage delete(Long id) {
        Assert.notNull(id, "id 不能为空");
        boolean delResult = carManageService.delById(id);
        if (!delResult) {
            return RestBuilders.failureBuilder().message("失败").build();
        }
        return RestBuilders.successBuilder().message("成功").build();
    }

    @PostMapping("exportData")
    @ApiOperation("导出")
    public void exportData(@RequestBody CarManage carManage, HttpServletRequest request, HttpServletResponse response) {
        carManageService.exportData(carManage, request, response);
    }

    @GetMapping("getImportTemplateSpace")
    @ApiOperation("下载导入模板")
    public void getImportTemplateSpace(HttpServletRequest request, HttpServletResponse response) {
        carManageService.getImportTemplateSpace(request, response);
    }

    @PostMapping("importData")
    public RestMessage importData(@RequestParam(value = "file") MultipartFile file) {
        Assert.notNull(file, "文件不能为空");

        Map<Integer, String> resultMap= carManageService.importData(file);
        if(resultMap.isEmpty()) {
            return RestBuilders.successBuilder().data("导入成功").build();
        }else {
            Collection<String> values = resultMap.values();
            StringBuilder errorMsgTemp = new StringBuilder();
            values.forEach(error -> {
                errorMsgTemp.append(";").append(error);
            });
            String errorMsg = errorMsgTemp.toString().substring(1);
            return RestBuilders.successBuilder().data(errorMsg).build();
        }
    }

}
